package com.edooteach.api.web.controller.shopping;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.demo2do.core.entity.result.HttpResult;
import com.edoomath.entity.user.UserAddress;
import com.edooteach.api.dto.UserAddressEdit;
import com.edooteach.api.servcice.UserAddressApiService;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/user-addresses")
public class UserAddressApiController {
    
    private static final Logger logger = LoggerFactory.getLogger(UserAddressApiController.class);
    
    @Autowired
    private UserAddressApiService userAddressApiService;
    
    // api1: list all address by unionid;
    
    /**
     * 
     * @param unionid
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult list(@RequestParam(value = "unionid", required = true) String unionid) {
        
        HttpResult httpResult = new HttpResult();
        
        List<UserAddress> userAddresses = userAddressApiService.list(unionid);
        
        return httpResult.success().data("userAddresses", userAddresses).message("获取所有地址成功");
        
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/default", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult loadDefault(@RequestParam(value = "unionid", required = true) String unionid) {
        HttpResult httpResult = new HttpResult();
        UserAddress userAddress = userAddressApiService.loadDefault(unionid);
        return httpResult.success().data("userAddresse", userAddress).message("获取默认地址成功");
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/{id}/default", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult setDefault(@PathVariable("id") Long addressId, @RequestParam(value = "unionid", required = true) String unionid) {
        HttpResult httpResult = new HttpResult();
        UserAddress userAddress = userAddressApiService.setDefault(addressId, unionid);
        return httpResult.success().data("userAddresse", userAddress).message("修改默认地址成功");
    }
    
    // api2: load address by id
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult loadById(@PathVariable("id") Long id) {
        
        HttpResult httpResult = new HttpResult();
        
        UserAddress userAddress = userAddressApiService.load(UserAddress.class, id);
        
        return httpResult.success().data("userAddress", userAddress).message("获取地址成功");
    }
    
    
    // api3: create by user & reciver & reciverMobile & province & city & district & address
    /**
     * 
     * @param userAddressRequest
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult create(@RequestParam(value = "unionid", required = true) String unionid, @RequestBody UserAddressEdit userAddressEdit) {
        
        HttpResult httpResult = new HttpResult();
        try {
            userAddressApiService.create(unionid, userAddressEdit);
        
            httpResult.success().message("创建成功");
        
        } catch (Exception e) {
            
            logger.error("#create", e);
        
            httpResult.fail("500").message("创建失败");
        }
        
        return httpResult;
    }
    
    // api4: update address, @RequestParam Adress 
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult editAddress(@PathVariable("id") Long addressId, @RequestBody UserAddressEdit userAddressEdit) {
        
        HttpResult httpResult = new HttpResult();
        try {
            userAddressApiService.edit(addressId, userAddressEdit);
        
            httpResult.success().message("编辑成功");
        
        } catch (Exception e) {
            
            logger.error("#create", e);
        
            httpResult.fail("500").message("编辑失败");
        }
        
        return httpResult;
    }
    
    // api5: delete by id
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult delete(@PathVariable("id") Long id) {
        HttpResult httpResult = new HttpResult();
        try {
            
            userAddressApiService.delete(id);
        
            httpResult.success().message("删除成功");
        
        } catch (Exception e) {
        
            logger.error("delete- ", e);
            httpResult.fail("500").message("删除失败");
        }
        
        return httpResult;
    }

}
